package com.match.plugin.kafka;

import java.math.BigDecimal;
import java.util.Date;
import java.util.Properties;
import java.util.concurrent.ExecutionException;
import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerRecord;
import com.match.common.model.dto.CancelOrderParam;
import com.match.common.model.dto.Order;
import com.match.util.SnowflakeIdWorker;
import cn.hutool.json.JSONUtil;

public class TestKafkaProducer {
	public static void main(String[] args) throws InterruptedException, ExecutionException {
        //添加生产者
		Properties props = new Properties();
    	props.put("bootstrap.servers", "127.0.0.1:9092");
        props.put("key.serializer", "org.apache.kafka.common.serialization.LongSerializer");
        props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");
        props.put("batch.size", 0);
		KafkaProducer<Long, String> producer = new KafkaProducer<Long, String>(props);
        
//        for (long i = 0; i<8; i++){
        
		Order order = new Order();
        Long id = SnowflakeIdWorker.generateId();
		order.setId(id);
		order.setUid(SnowflakeIdWorker.generateId());
		order.setIsBuy(true);
		order.setPrice(new BigDecimal(10));
		order.setNumber(new BigDecimal(1));
		order.setIsMarket(Boolean.FALSE);//true:市价；false：限价
		order.setCoinTeam("XBIT-USDT");
		order.setDecimalNumber(4);
		order.setTotalPrice(order.getPrice().multiply(order.getNumber()));
		order.setFinishNumber(BigDecimal.ZERO);
		order.setUnFinishNumber(order.getNumber());
		order.setState(0);
		order.setCreateTime(new Date());
		order.setSurplusFrozen(order.getTotalPrice());// 剩余冻结
		System.out.println(id);
		producer.send(new ProducerRecord<Long, String>("matchOrder", order.getId(), JSONUtil.toJsonStr(order)));

		CancelOrderParam order1 = new CancelOrderParam();
		order1.setId(id);
		order1.setIsBuy(true);
		order1.setCoinTeam("XBIT-USDT");
//		Long id1 = SnowflakeIdWorker.generateId();
//		order1.setId(id1);
//		order1.setUid(SnowflakeIdWorker.generateId());
//		order1.setIsBuy(false);
//		order1.setPrice(new BigDecimal(10));
//		order1.setNumber(new BigDecimal(1));
//		order1.setIsMarket(Boolean.FALSE);
//		order1.setCoinTeam("XBIT-USDT");
//		order1.setDecimalNumber(4);
//		order1.setTotalPrice(order1.getPrice().multiply(order1.getNumber()));
//		order1.setFinishNumber(BigDecimal.ZERO);
//		order1.setUnFinishNumber(order1.getNumber());
//		order1.setState(0);
//		order1.setCreateTime(new Date());
//		order1.setSurplusFrozen(order1.getUnFinishNumber());// 剩余冻结
//		System.out.println(id1);
		producer.send(new ProducerRecord<Long, String>("revokeOrder", order1.getId(), JSONUtil.toJsonStr(order1)));
//        }
		
		producer.close();
        
//        TimerUtil.init();
//		for (long i = 0; i<50; i++){
//	        Random random = new Random();
//			int price = random.nextInt(100) + 1;
//
//			MatchOrder order = new MatchOrder();
//			order.setId(SnowflakeIdWorker.generateId());
//			order.setUid(SnowflakeIdWorker.generateId());
//			order.setIsBuy(RandomUtil.randomBoolean());
//			order.setPrice(new BigDecimal(price));
//			order.setNumber(BigDecimal.ONE);
//			order.setTotalPrice(BigDecimal.ONE);
//			order.setIsMarket(Boolean.FALSE);
//			order.setCoinTeam("XBIT-USDT");
//			order.setDecimalNumber(4);
//			if (order.getIsMarket()) {
//				order.setTotalPrice(order.getTotalPrice());
//			} else {
//				order.setTotalPrice(order.getPrice().multiply(order.getNumber()));
//			}
//			order.setFinishNumber(BigDecimal.ZERO);
//			order.setUnFinishNumber(order.getNumber());
//			order.setState(0);
//			order.setCreateTime(new Date());
//			if (order.getIsBuy()) {
//				order.setSurplusFrozen(order.getTotalPrice());// 剩余冻结
//			} else {
//				order.setSurplusFrozen(order.getUnFinishNumber());// 剩余冻结
//			}
//			
//			
//			producer.send(new ProducerRecord<Long, String>("matchOrder", order.getId(), JSONUtil.toJsonStr(order)));
////			System.out.println("发送需要撮合的订单");
////			System.out.println(f.get());
//		}
//		System.out.println(TimerUtil.getDuration());
        
    }
	

	
}
